package com.ibm;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ibm.db.IBMETPer;

/**
 * Servlet implementation class for Servlet: Write2DB
 *
 */
 public class Write2DB extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   SimpleDateFormat sdf = null;
   
   private String formatStr(String str) {
		return str == null ? "" : str;
	}
   
	public Write2DB() {
		super();
	}   	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String name = formatStr(request.getParameter("name"));
		String age = formatStr(request.getParameter("age"));
		
		
//		String birthday = formatStr(request.getParameter("birthday"));
		sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date birthday = null;
		try {
			birthday = sdf.parse(request.getParameter("birthday"));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String ibmerID= formatStr(request.getParameter("ibmerID"));
		String major= formatStr(request.getParameter("major"));
		String certificateType= formatStr(request.getParameter("certificateType"));
		String college= formatStr(request.getParameter("college"));
		
		IBMETPer ibmer=new IBMETPer();
		
		ibmer.setName(name);
		ibmer.setAge(Integer.parseInt(age));
		ibmer.setBirthday(birthday);
		ibmer.setIbmerID(ibmerID);
		ibmer.setMajor(major);
		ibmer.setCertificateType(certificateType);
		ibmer.setCollege(college);
		
		
//		System.out.println(ibmer.getAge());
		DBoperation db = new DBoperation();
		try {
			db.persit(ibmer);  //插入数据
			String str="数据插入成功!";
			HttpSession session = request.getSession();
			session.setAttribute("info",str);
			System.out.println("数据插入成功!");
			response.sendRedirect("input.jsp");
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		
	}   	  	    
}